package com.yzg.simple.netty.chat.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @ClassName NettyChatClient
 * @Description TODO
 * @Authoe weiyan
 * @Date 2019/12/30 10:57
 * @Version 1.0
 **/
public class NettyChatClient {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup clientGroups = new NioEventLoopGroup(1);

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(clientGroups)
                       .channel(NioSocketChannel.class)
                        .handler(new NettyChatClientInitializer());

            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6863).sync();

            channelFuture.addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println("客户端启动成功 " + channelFuture.channel().localAddress());
                }


            });

            channelFuture.channel().closeFuture().sync();

        }finally {
            clientGroups.shutdownGracefully();
        }
    }

}